<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class messages extends CI_Controller {

	function __construct() {
		// Need to run CI_Controller constructor
		parent::__construct();
		// Load the user model
		// This is needed for checking user is logged in and has permission to access the page
		$this->load->model('user');
		$this->load->model('groups');
		$this->load->model('group_message');
	}

	/**
	 * Account management homepage
	 */
	public function index() {

		// Check if the user is logged in
		if ( ! $this->user->is_logged()  ) { redirect('welcome/login'); }
		
		// Page title to send to view
		$data['page_title'] = "Group Messages - Blueboard";
		// Page content 
		$data['main_content'] = "messages/group_messages";
		
		// load the view
		$this->load->view('template', $data);
	}
	
	public function addAdminMessage() {
		if ( ! $this->user->is_logged() ) { redirect('welcome/login'); }
		if ( ! $this->user->is_lecturer() AND ! $this->user->is_administrator()) { redirect('messages'); }
		
		if ($this->uri->segment(3) == 'add') {
			if($text = $this->input->post('messageText')) {
				$this->group_message->addAdminMessage($this->session->userdata('user_id'), $text);
				redirect('welcome');
			}
		} else {
			// Page title to send to view
			$data['page_title'] = "Add a Unit Announcement";
			// Page content 
			$data['main_content'] = "messages/add_admin_message";

			// Template Settings
			$data['content_title'] = 'Add a Unit Announcement';
			$data['left_sidebar'] = 'admin_links';
			$data['sidebar_modifier'] = 'Hide Right';
			$data['left_sidebar_title'] = 'Administration';
		
			// load the view
			$this->load->view('template', $data);
		}
	}
	
	public function removeAdminMessage() {
		if ( ! $this->user->is_logged() ) { redirect('welcome/login'); }
		if ( ! $this->user->is_lecturer() AND ! $this->user->is_administrator()) { redirect('messages'); }
		
		if ($this->uri->segment(3) == 'remove') {
			$this->group_message->remove_message($this->uri->segment(4));
		} else {
			// Page title to send to view
			$data['page_title'] = "Add Admin announcement - Blueboard";
			// Page content 
			$data['main_content'] = "messages/remove_message";
		
			// load the view
			$this->load->view('template', $data);
		}
	}
	
	public function viewGroupMessages() {
		if ( ! $this->user->is_logged() ) { redirect('welcome/login'); }
		$userID = $this->session->userdata('user_id');
		$groups = $this->groups->get_student_groups($userID);
		
		foreach ($groups as $key => $group) {
			$group_id = $group['group_id'];
		}
		
		$messages = $this->group_message->getGroupMessages($group_id);
		
		// Page title to send to view
		$data['page_title'] = "Group Messages";
		// Page content 
		$data['main_content'] = "messages/group_messages";
		//Group ID
		$data['group_messages'] = $messages;

		// Template Settings
		$data['content_title'] = 'Groups';
		$data['left_sidebar'] = 'my_group';
		$data['sidebar_modifier'] = 'Hide Right';
		$data['left_sidebar_title'] = 'My Group';
		
		// load the view
		$this->load->view('template', $data);
	}
	
	public function addGroupMessage() {
		if ( ! $this->user->is_logged() ) { redirect('welcome/login'); }
		$userID = $this->session->userdata('user_id');
		$groups = $this->groups->get_student_groups($userID);
		
		foreach ($groups as $key => $group) {
			$group_id = $group['group_id'];
		}
		
		if ($this->uri->segment(3) == 'add') {
			if($text = $this->input->post('messageText')) {
				$this->group_message->addGroupMessage($group_id, $this->session->userdata('user_id'), $text);
				redirect('welcome');
			}
		} else {
			// Page title to send to view
			$data['page_title'] = "Add Group Message - Blueboard";
			// Page content 
			$data['main_content'] = "messages/add_group_message";

			// Template Settings
			$data['content_title'] = 'Groups';
			$data['left_sidebar'] = 'my_group';
			$data['sidebar_modifier'] = 'Hide Right';
			$data['left_sidebar_title'] = 'My Group';
		
			// load the view
			$this->load->view('template', $data);
		}
	}
	
	public function addRandomMessage() {
		$this->group_message->addGroupMessage(1, $this->session->userdata('user_id'), "Hello there");
		$redirect('welcome');
	}
}